package com.um.jdy.platform.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.commons.service.MobileService;
import com.um.jdy.common.member.enums.CodeKey;
import com.um.jdy.common.platform.code.PlatformCode;
import com.um.jdy.common.platform.entity.po.CompanyUser;
import com.um.jdy.common.platform.entity.po.DepositFree;
import com.um.jdy.common.platform.entity.vo.DepositFreeVO;
import com.um.jdy.common.platform.service.CompanyUserService;
import com.um.jdy.common.platform.service.DepositFreeService;
import com.um.jdy.platform.platform.entity.dto.DepositFreeCodeDTO;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 管理类：应用信息设置
 */
@Component
public class DepositFreeManager extends BaseManager<DepositFreeService> {

    @Autowired
    MobileService mobileService;

    @Autowired
    CompanyUserService companyUserService;

    /**
     * 详情
     *
     * @return
     */
    public Response<DepositFreeVO> get() {
        DepositFree depositFree = service.getEntity("com_id", getComId());

        if(depositFree == null){
            depositFree = new DepositFree();
            depositFree.setComId(getComId());
            depositFree.setOpenWxScore(YesOrNo.No.name());
            depositFree.setWxServiceId("");
            depositFree.setOpenAlipayScore(YesOrNo.No.name());
            depositFree.setAlipayServiceId("");
            service.save(depositFree);
        }

        DepositFreeVO depositFreeVO = UMBeanUtils.copyBean(depositFree,DepositFreeVO.class);
        depositFreeVO.setAlipayServiceId(repaceAll(depositFreeVO.getAlipayServiceId()));
        depositFreeVO.setWxServiceId(repaceAll(depositFreeVO.getWxServiceId()));

        return Response.success(depositFreeVO);
    }

    /**
     * 更新
     *
     * @param id
     * @param depositFreeCodeDTO
     * @return
     */
    public Response update(String id, DepositFreeCodeDTO depositFreeCodeDTO) {

        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        mobileService.checkCode(CodeKey.EditSetting, master.getMobile(), depositFreeCodeDTO.getCode());

        DepositFree depositFree = BeanUtil.copyProperties(depositFreeCodeDTO, DepositFree.class);
        depositFree.setPlatformDepositFreeId(id);
        service.updateEntity(depositFree);
        return Response.success();
    }

    /**
     * 替换字符
     * @param value
     * @return
     */
    private String repaceAll(String value){
        if(value == null || value.length() == 0){
            return value;
        }

        String start = "";
        if(value.length() > 4)
            start = value.substring(0,4);
        else
            start = value.substring(0);

        String end = "";
        if(value.length() > 4)
            end = value.substring(value.length() - 4);

        StringBuilder sb = new StringBuilder();
        sb.append(start);

        for(int index = 0;index < value.length() - 8;index ++){
            sb.append("*");
        }

        sb.append(end);
        return sb.toString();
    }

}
